package com.tuanchau.android.englishtooltip.ui.activity;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.tuanchau.android.englishtooltip.R;
import com.tuanchau.android.englishtooltip.database.ScaleWord;

public class ChooseWordDialog extends Dialog implements OnItemClickListener,
		OnItemLongClickListener {

	private final ListView listView;
	private final ScaleWord[] list;
	private final Context context;

	public ChooseWordDialog(Context context, String word, ScaleWord[] list) {
		super(context);
		this.context = context;
		setContentView(R.layout.dialog_edit_word_choose);
		setTitle(word);
		this.list = list;

		listView = (ListView) findViewById(R.id.dialog_edit_word_choose_list);
		listView.setAdapter(new MeanAdapter(context, list));
		listView.setOnItemClickListener(this);

	}

	private class MeanAdapter extends ArrayAdapter<ScaleWord> {

		private final ScaleWord[] list;

		public MeanAdapter(Context context, ScaleWord[] list) {
			super(context, R.layout.dialog_edit_word_choose_item, list);
			this.list = list;

		}

		@Override
		public View getView(int position, View v, ViewGroup parent) {
			if (v == null) {
				final LayoutInflater inflater = getLayoutInflater();
				v = inflater.inflate(R.layout.dialog_edit_word_choose_item,
						null);
				

				
			}
			final TextView txtMean = (TextView) v
					.findViewById(R.id.dialog_edit_word_choose_item_mean);
			final TextView txtType = (TextView) v
					.findViewById(R.id.dialog_edit_word_choose_item_type);
			final ScaleWord word = list[position];
			txtMean.setText(word.getMean());
			txtType.setText(word.getType());
			return v;
		}

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
		((EditWordActivity) context).setMean(list[position].getType(),
				list[position].getMean());
		this.dismiss();
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View v, int position,
			long id) {
		return false;
	}
}
